package p0;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * @author pengjz <br>
 * @version <br>
 * @name Leetcode_14 <br>
 * @date 2020/5/30 0:42<br>
 */
public class Leetcode_18 {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> result = new ArrayList<>();
        Arrays.sort(nums);
        for (int i = 0; i < nums.length - 3; i++) {
            for (int j = i + 1; j < nums.length - 2; j++) {
                for (int k = j + 1; k < nums.length - 1; k++) {
                    for (int l = k + 1; l < nums.length; l++) {
                        if (nums[i] + nums[j] + nums[k] + nums[l] == target) {
                            List<Integer> list = new ArrayList<>();
                            list.add(nums[i]);
                            list.add(nums[j]);
                            list.add(nums[k]);
                            list.add(nums[l]);
                            if (!isExist(result, list)) {
                                result.add(list);
                            }
                        }
                    }
                }
            }
        }
        return result;
    }

    private boolean isExist(List<List<Integer>> listList, List<Integer> list) {
        if (listList.size() == 0) {
            return false;
        }
        for (List<Integer> integers : listList) {
            if (integers.size() != list.size()) {
                return false;
            }
            if (integers.get(0).equals(list.get(0))
                    && integers.get(1).equals(list.get(1))
                    && integers.get(2).equals(list.get(2))
                    && integers.get(3).equals(list.get(3))
            ) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Leetcode_18 leetcode = new Leetcode_18();
        System.out.println(leetcode.fourSum(new int[]{-3, -2, -1, 0, 0, 1, 2, 3}, 0));
    }
}
